#include "duplicatesdialog.h"
#include "ui_duplicatesdialog.h"
#include <QMessageBox>
#include <QThread>
#include <QTimer>

class MediaItemTreeWidgetItem : public QTreeWidgetItem
{
public:
    MediaItemTreeWidgetItem(QTreeWidgetItem * parent, MediaItemPtr mi)
        : QTreeWidgetItem(parent, QStringList() << mi->metadata().filename),
          m_mi(mi)
    {
    }

    MediaItemPtr item() { return m_mi; }

private:
    MediaItemPtr m_mi;
};

DuplicatesDialog::DuplicatesDialog(PioCore * core, QList<QSet<MediaItemPtr> > groups, QWidget *parent) :
    QDialog(parent),
    m_core(core),
    ui(new Ui::DuplicatesDialog),
    m_groups(groups)
{
    ui->setupUi(this);
    ui->treeWidget->setIconSize(QSize(256, 256));
    for(int i=0; i<groups.size(); i++) {
        QTreeWidgetItem * parent = new QTreeWidgetItem(ui->treeWidget, QStringList() << QString("item %1").arg(i+1));
        QList<MediaItemPtr> list = groups[i].toList();
        qSort(list.begin(), list.end(), lessFilename);
        for(int j=0; j<list.size(); j++) {
            QTreeWidgetItem * item = new MediaItemTreeWidgetItem(parent, list[j]);
            QFont f = item->font(0);
            f.setBold(true);
            item->setFont(0, f);
        }
        parent->setExpanded(true);
    }
    QTimer::singleShot(100, this, SLOT(timeout()));
}

DuplicatesDialog::~DuplicatesDialog()
{
    delete ui;
}

void DuplicatesDialog::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int )
{
    select(item);
}

void DuplicatesDialog::on_treeWidget_itemPressed(QTreeWidgetItem *item, int )
{
    select(item);
}

void DuplicatesDialog::select(QTreeWidgetItem * item)
{
    QFont f = item->font(0);
    QTreeWidgetItem * parent = item->parent();
    if(parent == 0) {
        for(int i=0; i<item->childCount(); i++) {
            f.setBold(true);
            item->child(i)->setFont(0, f);
            item->child(i)->setForeground(0, Qt::black);
        }
    }
    else {
        for(int i=0; i<parent->childCount(); i++) {
            f.setBold(parent->child(i) == item);
            parent->child(i)->setFont(0, f);
            parent->child(i)->setForeground(0, parent->child(i) == item ? Qt::black : Qt::red);
        }
    }
}

void DuplicatesDialog::on_buttonNewest_pressed()
{
    for(int i=0; i<ui->treeWidget->topLevelItemCount(); i++) {
        QTreeWidgetItem * parent = ui->treeWidget->topLevelItem(i);
        MediaItemTreeWidgetItem * newest = (MediaItemTreeWidgetItem*)parent->child(0);
        for(int i=1; i<parent->childCount(); i++) {
            MediaItemTreeWidgetItem * item = (MediaItemTreeWidgetItem*)parent->child(i);
            if(item->item()->metadata().modified < item->item()->metadata().modified)
                newest = item;
        }
        for(int i=0; i<parent->childCount(); i++) {
            QFont f = parent->child(i)->font(0);
            f.setBold(parent->child(i) == newest);
            parent->child(i)->setFont(0, f);
            parent->child(i)->setForeground(0, parent->child(i) == newest ? Qt::black : Qt::red);
        }
    }
}

void DuplicatesDialog::on_buttonKeepAll_pressed()
{
    for(int i=0; i<ui->treeWidget->topLevelItemCount(); i++) {
        QTreeWidgetItem * parent = ui->treeWidget->topLevelItem(i);
        for(int i=0; i<parent->childCount(); i++) {
            QFont f = parent->child(i)->font(0);
            f.setBold(true);
            parent->child(i)->setFont(0, f);
            parent->child(i)->setForeground(0, Qt::black);
        }
    }
}

void DuplicatesDialog::on_buttonBox_accepted()
{
    QList<MediaItemPtr> items;
    for(int i=0; i<ui->treeWidget->topLevelItemCount(); i++) {
        QTreeWidgetItem * parent = ui->treeWidget->topLevelItem(i);
        for(int i=0; i<parent->childCount(); i++) {
            MediaItemTreeWidgetItem * item = (MediaItemTreeWidgetItem*)parent->child(i);
            if(!item->font(0).bold())
                items.append(item->item());
        }
    }
    if(items.size()==0)
        return;
    if(QMessageBox::question(this, "Really delete?", QString("Do you really want to delete %1 items?").arg(items.size()), QMessageBox::Yes, QMessageBox::No)==QMessageBox::Yes) {
        foreach(MediaItemPtr item, items)
            m_core->trash(item);
    }
}

void DuplicatesDialog::timeout()
{
    for(int i=0; i<ui->treeWidget->topLevelItemCount(); i++) {
        QTreeWidgetItem * parent = ui->treeWidget->topLevelItem(i);
        if(parent->icon(0).isNull()) {
            MediaItemTreeWidgetItem * item = (MediaItemTreeWidgetItem*)parent->child(0);
            parent->setIcon(0, m_core->icon(item->item(), QSize(256, 256), true));
            QTimer::singleShot(100, this, SLOT(timeout()));
            break;
        }
    }
}

bool DuplicatesDialog::lessFilename(MediaItemPtr a, MediaItemPtr b)
{
    return a->metadata().filename < b->metadata().filename;
}
